import java.util.*;

/**
 * The type Recipe collection.
 *
 * @author Yihan Wang
 * @date 24/06/11
 */
public class RecipeCollection {
    /**
     * The Rc.
     */
    public ALSet<Recipe> rc;
    ////////////////////////////////////////////////

    /**
     * Instantiates a new Recipe collection.
     */
    public RecipeCollection() {
        this.rc = new ALSet<>();
    }

    /**
     * Instantiates a new Recipe collection.
     *
     * @param rc the rc
     */
    public RecipeCollection(ALSet<Recipe> rc) {
        this.rc = rc;
    }
    ////////////////////////////////////////////////

    /**
     * Gets rc.
     *
     * @return the rc
     */
    public ALSet<Recipe> getRc() {
        return rc;
    }

    /**
     * Sets rc.
     *
     * @param rc the rc
     */
    public void setRc(ALSet<Recipe> rc) {
        this.rc = rc;
    }
    ////////////////////////////////////////////////

    /**
     * Add recipe.
     *
     * @param r the r
     */
    public void addRecipe(Recipe r) {
        rc.add(r);
    }

    /**
     * Gets top 3 recipes.
     *
     * @return the top 3 recipes
     */
    public List<Recipe> getTop3Recipes() {
        updateAllRecipe(Main.fc1);
        PriorityQueue<Recipe> topRecipes = new PriorityQueue<>(Comparator.comparingInt(Recipe::getMatchingCnt));

        for (Recipe recipe : rc) {
            topRecipes.offer(recipe);
            if (topRecipes.size() > 3) {
                topRecipes.poll();
            }
        }

        List<Recipe> result = new ArrayList<>(topRecipes);
        result.sort(Comparator.comparingInt(Recipe::getMatchingCnt).reversed());
        return result;
    }

    /**
     * Update all recipe.
     *
     * @param fc the fc
     */
    public void updateAllRecipe(FoodCollection fc) {
        for (Recipe recipe : rc) {
            recipe.updateMatchingCount(fc);
        }
    }

    /**
     * Search recipe recipe.
     *
     * @param recipeName the recipe name
     * @return the recipe
     */
    public Recipe searchRecipe(String recipeName) {
        for (Recipe recipe : rc) {
            if (recipeName.equalsIgnoreCase(recipe.getRecipeName())) {
                return recipe;
            }
        }
        return null;
    }

    /**
     * Cook recipe string.
     *
     * @param fc the fc
     * @param r  the r
     * @return the string
     */
    public String cookRecipe(FoodCollection fc, Recipe r) {
        return r.cookIngredient(fc);
    }
}
